package mia.beans;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.hibernate.Session;

import mia.dao.UserEntityDAO;
import mia.entity.UserEntity;
import mia.util.DigesterUtil;
import mia.util.HibernateManager;

@ManagedBean(name="userEditBean")
@RequestScoped
public class UserEditBean {

	@ManagedProperty("#{userLoginBean.user}")
	private UserEntity user;
	
	private String passwordOld;
	private String passwordNew;
	private String passwordNew2;
	private String email2;
	
	private String editStatus = "";

	public UserEntity getUser() {
		return user;
	}
	public void setUser(UserEntity user) {
		this.user = user;
	}
	public String getPasswordOld() {
		return passwordOld;
	}
	public void setPasswordOld(String passwordOld) {
		this.passwordOld = passwordOld;
	}
	public String getPasswordNew2() {
		return passwordNew2;
	}
	public void setPasswordNew2(String passwordNew2) {
		this.passwordNew2 = passwordNew2;
	}	
	public String getPasswordNew() {
		return passwordNew;
	}
	public void setPasswordNew(String passwordNew) {
		this.passwordNew = passwordNew;
	}
	public String getEditStatus() {
		return editStatus;
	}
	public void setEditStatus(String editStatus) {
		this.editStatus = editStatus;
	}
	public String getEmail2() {
		return email2;
	}
	public void setEmail2(String email2) {
		this.email2 = email2;
	}
	
	public void tryEdit()
	{
		FacesContext context = FacesContext.getCurrentInstance();
		if(DigesterUtil.checkPassword(passwordOld, user.getPassword()))
		{
			
			if(passwordNew2.equals(passwordNew))
			{

				if(!user.getEmail().equals(email2) && !email2.isEmpty() )
				{
					context.addMessage("change",new FacesMessage(FacesMessage.SEVERITY_ERROR, "Wpisane adresy e-mail nie są identyczne!",""));
				}
				else
				{
					if(email_validate(user.getEmail()))
					{
						String passwordNewEncrypted="";
						if(!passwordNew.isEmpty()){ passwordNewEncrypted = DigesterUtil.getDigest(passwordNew);
						user.setPassword(passwordNewEncrypted);}
						Session session = HibernateManager.getSession();
						session.beginTransaction();
						UserEntity u = UserEntityDAO.getUserByLogin(session, user.getLogin());
				
						if(!u.getEmail().equals(user.getEmail()))
							u.setEmail(user.getEmail());
			
						if(!u.getFirstName().equals(user.getFirstName()))
							u.setFirstName(user.getFirstName());
				
						if(!u.getLastName().equals(user.getLastName()))
							u.setLastName(user.getLastName());
				
						if(!passwordNewEncrypted.isEmpty() && !u.getPassword().equals(passwordNewEncrypted))
							u.setPassword(passwordNewEncrypted);
				
						if(!u.getPhoneNumber().equals(user.getPhoneNumber()))
							u.setPhoneNumber(user.getPhoneNumber());
				
						try
						{
							session.save(u);
							session.getTransaction().commit();
							context.addMessage("change",new FacesMessage(FacesMessage.SEVERITY_INFO, "Dane zostały zmienione!",""));
						}
						catch(Exception ex)
						{
							context.addMessage("change",new FacesMessage(FacesMessage.SEVERITY_ERROR, "Wystąpił błąd: spróbuj ponownie",""));
							session.getTransaction().rollback();
						}
						finally
						{
							session.close();
						}
					}
					else
					{
						context.addMessage("change",new FacesMessage(FacesMessage.SEVERITY_ERROR, "Podano niepoprawny adres e-mail",""));
					}
				}
				
			}
		}
		else
		{
			context.addMessage("change",new FacesMessage(FacesMessage.SEVERITY_ERROR, "Stare hasło nie jest poprawne!",""));
		}
	}
	private Boolean email_validate(String email)
	{
		Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
		Matcher m = p.matcher(email);
		if(m.matches())
			return true;
		else
			return false;
	}
}
